package com.example.administrator.runbook.findfragment.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.administrator.runbook.R;

import java.util.List;

/**
 * Created by 俊峰 on 2016/11/7.
 */

public class MyBaseAdapter extends BaseAdapter {

    private List<String> mStringList;
    private Context mContext;
    private LayoutInflater mLayoutInflater;
    private int mHeight;

    public MyBaseAdapter(List<String> stringList, Context context, int height) {
        mStringList = stringList;
        mContext = context;
        mLayoutInflater = LayoutInflater.from(this.mContext);
        mHeight = height;
    }

    @Override
    public int getCount() {
        return mStringList.size();
    }

    @Override
    public Object getItem(int position) {
        return mStringList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View ret;
        if (convertView == null) {
            ret = mLayoutInflater.inflate(R.layout.fragment_find_list_item, parent, false);
        } else {
            ret = convertView;
        }
        ViewHolder holder = (ViewHolder) ret.getTag();
        if (holder == null) {
            holder = new ViewHolder(ret);
            ret.setTag(holder);
        }
        onBindView(holder, mStringList.get(position), mStringList.size());
        return ret;
    }

    private void onBindView(ViewHolder holder, String result, int count) {

        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) holder.myAll.getLayoutParams();
        params.height = mHeight / count;
        holder.myAll.setLayoutParams(params);
        holder.itemText.setText(result);
        holder.lastImage.setImageResource(R.mipmap.new_buy_right_arrow);
        switch (result) {
            case "排行榜":
                holder.firstImage.setImageResource(R.mipmap.home_find_rank);
                break;
            case "分类":
                holder.firstImage.setImageResource(R.mipmap.home_find_category);
                break;
            case "主题书单":
                holder.firstImage.setImageResource(R.mipmap.home_find_topic);
                break;
            case "有声小说":
                holder.firstImage.setImageResource(R.mipmap.home_find_listen);
                break;
            case "神秘功能":
                holder.firstImage.setImageResource(R.mipmap.home_find_secret_unlocked);
                break;
            case "漫画中心":
                holder.firstImage.setImageResource(R.mipmap.home_find_month);
                break;
            case "游戏中心":
                holder.firstImage.setImageResource(R.mipmap.home_find_game_center);
                break;
            case "咪咕阅读":
                holder.firstImage.setImageResource(R.mipmap.home_find_migu);
                break;
            case "一元得宝":
                holder.firstImage.setImageResource(R.mipmap.home_find_oneyuan);
                break;
            case "情感热线":
                holder.firstImage.setImageResource(R.mipmap.homefind_qinggan);
                break;
        }

    }

    private static class ViewHolder{

        private ImageView firstImage;
        private TextView itemText;
        private ImageView lastImage;
        private View myAll;

        public ViewHolder(View itemView){
            firstImage = (ImageView) itemView.findViewById(R.id.fragment_find_list_item_img_first);
            itemText = (TextView) itemView.findViewById(R.id.fragment_find_list_item_text);
            lastImage = (ImageView) itemView.findViewById(R.id.fragment_find_list_item_img_last);
            myAll = itemView.findViewById(R.id.fragment_find_list_item_some);
        }

    }

}
